package com.baidu.foodrecipe.ui.fragment.recipes

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baidu.foodrecipe.R
import com.baidu.foodrecipe.base.BaseFragment
import com.baidu.foodrecipe.databinding.FragmentRecipesBinding
import com.baidu.foodrecipe.ui.adapters.RecipesAdapter

class RecipesFragment: BaseFragment<FragmentRecipesBinding>() {
    private val mainViewModel: MainViewModel by activityViewModels()
    private val mAdapter: RecipesAdapter = RecipesAdapter()
    private val mNetworkAvailableObserver: Observer<Boolean> by lazy {
        object : Observer<Boolean> {
            override fun onChanged(value: Boolean) {
                if (value){
                    mBinding.noInternetContainer.visibility = View.GONE
                    mBinding.recyclerView.visibility = View.VISIBLE
                    //移除监听器 只监听到有网就不监听了
                    mainViewModel.isNetworkAvailable.removeObserver(mNetworkAvailableObserver)
                }else{
                    mBinding.noInternetContainer.visibility = View.VISIBLE
                    mBinding.recyclerView.visibility = View.INVISIBLE
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //加载数据
        mainViewModel.getRecipes()
    }

    override fun initUI(savedInstanceState: Bundle?) {

        //监听网络状态
        //创建一个匿名类对象
        mainViewModel.isNetworkAvailable.observe(viewLifecycleOwner,mNetworkAvailableObserver)

        //配置RecyclerView
        mAdapter.mainViewModel = mainViewModel
        mAdapter.lifecycleOwner = viewLifecycleOwner
        mBinding.recyclerView.adapter = mAdapter
        mBinding.recyclerView.layoutManager = LinearLayoutManager(
            requireActivity(),
            RecyclerView.VERTICAL,
            false
        )

        mainViewModel.showLoadAnimation.observe(viewLifecycleOwner) {
            if (it){
                Log.v("pxd","loading")
            }else{
                Log.v("pxd","finished")
            }
        }

        //监听MainViewModel中的数据
        mainViewModel.foodRecipe.observe(viewLifecycleOwner) {
            Log.v("pxd",it.results.toString())
            mAdapter.setNewData(it.results)
        }

        //给悬浮按钮添加点击事件
        mBinding.floatingActionButton.setOnClickListener {
            if (mainViewModel.isNetworkAvailable.value!!){
                //弹出底部弹窗
                findNavController().navigate(R.id.action_recipesFragment_to_searchTypeFragment2)
            }else{
                Toast.makeText(requireContext(),"No Internet Connection",Toast.LENGTH_SHORT).show()
            }
        }

        Log.v("pxd",mainViewModel.toString())
    }
}